//
//  TableVideoPlayer.swift
//  soapp
//
//  Created by zhu xietong on 2017/8/26.
//  Copyright © 2017年 CocoaPods. All rights reserved.
//

import Foundation
import Eelay
//import AsyncDisplayKit
//extension XLVideoPlayer{
//    public enum Notice:String,NotifyType {
//
//        case beginPlay = "beginPlay"
//
//
//        public var name: Notification.Name{
//            switch self {
//            case .beginPlay:
//                return "XLVideoPlayer.beginPlay".notice_name
//            }
//            //            return "JoUser.\(Notice.self)\(self.rawValue)".notice_name
//        }
//    }
//}

public protocol CellVideoSupport:NSObjectProtocol {
    var player:XLVideoPlayer?{get set}
    func stopVideo()
}

extension CellVideoSupport where Self:JoTableCell
{
    public func stopVideo() {
        self.player?.removeFromSuperview()
        self.player?.destroy()
        self.player = nil
    }
}
public protocol ListVideoSupport:NSObjectProtocol{
    func playVideoWith(videoKit:CellVideoSupport,cellModel:NSMutableDictionary,isCell:Bool)
    func removeCurrentPlayer()
    var playerV:XLVideoPlayer?{get set}
    //    var playerOnwer:CellVideoSupport?{get set}
}

extension ListVideoSupport where Self:ASListController
{
    
    
    public func removeCurrentPlayer() {
        self.playerV?.destroy()
        self.playerV = nil
    }
    
    public func playVideoWith(videoKit:CellVideoSupport,cellModel:NSMutableDictionary,isCell:Bool) {
                EeAudioPlayer.shared.stopAction()//关闭音频
        
//        __post(notify: XLVideoPlayer.Notice.beginPlay)
        if isCell//在cell上进行播放
        {
            
            self.playerV?.removeFromSuperview()
            self.playerV?.destroy()
            self.playerV = nil
            
            let list = DataNode.path(node: self.page_load_section, tableNode.model, value: NSMutableArray())
            let index = list.index(of: cellModel)
            let indexP = IndexPath(row: index, section: self.sectionIndex)
            
            let player = XLVideoPlayer()
            player.imGplay = VideoIMG.play
            player.imGpause = VideoIMG.pause
            player.imGzoomIn = VideoIMG.zoomin
            player.imGzoomOut = VideoIMG.zoomout
            self.playerV = player
            self.playerV?.videoUrl = cellModel["__video_url",""]
            self.playerV?.playerBindTableView(self.tableNode.view, currentIndexPath: indexP)
            //            self.playerV?.frame = [0,0,Swidth,(Swidth/2.0)]
            
            videoKit.player = self.playerV
            playerV?.completedPlayingBlock = {p in
                p?.destroy()
                videoKit.player = nil
            }
            
        }else{
            //            if let topV = videoKit as? TableTopVideoView
            //            {
            //
            //                self.playerV?.removeFromSuperview()
            //                self.playerV?.destroy()
            //                self.playerV = nil
            //
            //
            //                self.playerV = XLVideoPlayer()
            //                self.playerV?.videoUrl = cellModel["video",""]
            //                self.playerV?.playerBindTableView(topV.tableView, currentIndexPath: IndexPath(row: 0, section: 0))
            //
            //                videoKit.player = self.playerV
            //                weak var wself = self
            //                playerV?.completedPlayingBlock = {p in
            //                    p?.destroy()
            //                    wself?.playerV = nil
            //                }
            //
            //            }
            
        }
        
        
    }
}


extension ListVideoSupport where Self:ASColListController
{
    
    
    public func removeCurrentPlayer() {
        self.playerV?.destroy()
        self.playerV = nil
    }
    
    public func playVideoWith(videoKit:CellVideoSupport,cellModel:NSMutableDictionary,isCell:Bool) {
        EeAudioPlayer.shared.stopAction()//关闭音频
        
        print("----000000---",cellModel)
        //        __post(notify: XLVideoPlayer.Notice.beginPlay)
        if isCell//在cell上进行播放
        {
            
            print("+====---|",cellModel)
            self.playerV?.removeFromSuperview()
            self.playerV?.destroy()
            self.playerV = nil
            
//            let list = DataNode.path(node: self.page_load_section, collectionNode.model, value: NSMutableArray())
            let index = cellModel[int:"__row__",0]
            let section = cellModel[int:"__section__",0]

            
            let indexP = IndexPath(row: index, section: section)
            
            let player = XLVideoPlayer()
            
            player.imGplay = VideoIMG.play
            player.imGpause = VideoIMG.pause
            player.imGzoomIn = VideoIMG.zoomin
            player.imGzoomOut = VideoIMG.zoomout
            self.playerV = player
            self.playerV?.videoUrl = cellModel["__video_url",""]
            _ = collectionNode.cellForItem(at: indexP)
            
            
            
            self.playerV?.playerBindCollectionNode(collectionNode, currentIndexPath: indexP)
            
//            self.playerV?.playerBindColView(self.collectionNode, currentIndexPath: indexP)
//            self.playerV?.playerBindTableView(self.tableNode.view, currentIndexPath: indexP)
            //            self.playerV?.frame = [0,0,Swidth,(Swidth/2.0)]
            
            videoKit.player = self.playerV
            playerV?.completedPlayingBlock = {p in
                p?.destroy()
                videoKit.player = nil
            }
            
        }else{
            //            if let topV = videoKit as? TableTopVideoView
            //            {
            //
            //                self.playerV?.removeFromSuperview()
            //                self.playerV?.destroy()
            //                self.playerV = nil
            //
            //
            //                self.playerV = XLVideoPlayer()
            //                self.playerV?.videoUrl = cellModel["video",""]
            //                self.playerV?.playerBindTableView(topV.tableView, currentIndexPath: IndexPath(row: 0, section: 0))
            //
            //                videoKit.player = self.playerV
            //                weak var wself = self
            //                playerV?.completedPlayingBlock = {p in
            //                    p?.destroy()
            //                    wself?.playerV = nil
            //                }
            //
            //            }
            
        }
        
        
    }
}


extension ListVideoSupport where Self:ListController
{
    
    
    public func removeCurrentPlayer() {
        self.playerV?.destroy()
        self.playerV = nil
    }
    
    public func playVideoWith(videoKit:CellVideoSupport,cellModel:NSMutableDictionary,isCell:Bool) {
                EeAudioPlayer.shared.stopAction()//关闭音频
//        __post(notify: XLVideoPlayer.Notice.beginPlay)
        if isCell//在cell上进行播放
        {
            if let cell = videoKit as? JoTableCell
            {
                self.playerV?.removeFromSuperview()
                self.playerV?.destroy()
                self.playerV = nil
                
                let indexP = cell.__indexPath
                let player = XLVideoPlayer()
                player.imGplay = VideoIMG.play
                player.imGpause = VideoIMG.pause
                player.imGzoomIn = VideoIMG.zoomin
                player.imGzoomOut = VideoIMG.zoomout
                self.playerV = player
                
                self.playerV?.videoUrl = cellModel["__video_url",""]
                self.playerV?.playerBindTableView(self.tableView, currentIndexPath: indexP)
                //            self.playerV?.frame = [0,0,Swidth,(Swidth/2.0)]
                
                videoKit.player = self.playerV
                playerV?.completedPlayingBlock = {p in
                    p?.destroy()
                    videoKit.player = nil
                }
            }
            
        }else{
            //                        if let topV = videoKit as? TableTopVideoView
            //                        {
            //
            //                            self.playerV?.removeFromSuperview()
            //                            self.playerV?.destroy()
            //                            self.playerV = nil
            //
            //
            //                            self.playerV = XLVideoPlayer()
            //                            self.playerV?.videoUrl = cellModel["video",""]
            //                            self.playerV?.playerBindTableView(topV.tableView, currentIndexPath: IndexPath(row: 0, section: 0))
            //
            //                            videoKit.player = self.playerV
            //                            weak var wself = self
            //                            playerV?.completedPlayingBlock = {p in
            //                                p?.destroy()
            //                                wself?.playerV = nil
            //                            }
            //
            //                        }
            
        }
        
    }
    
}

extension ListVideoSupport where Self:PaginatorController
{
    
    
    public func removeCurrentPlayer() {
        self.playerV?.destroy()
        self.playerV = nil
    }
    
    public func playVideoWith(videoKit:CellVideoSupport,cellModel:NSMutableDictionary,isCell:Bool) {
                EeAudioPlayer.shared.stopAction()//关闭音频
//        __post(notify: XLVideoPlayer.Notice.beginPlay)
        if isCell//在cell上进行播放
        {
            if let cell = videoKit as? JoTableCell
            {
                self.playerV?.removeFromSuperview()
                self.playerV?.destroy()
                self.playerV = nil
                
                let indexP = cell.__indexPath
                let player = XLVideoPlayer()
                player.imGplay = VideoIMG.play
                player.imGpause = VideoIMG.pause
                player.imGzoomIn = VideoIMG.zoomin
                player.imGzoomOut = VideoIMG.zoomout
                self.playerV = player
                self.playerV?.videoUrl = cellModel["__video_url",""]
                self.playerV?.playerBindTableView(self.tableView, currentIndexPath: indexP)
                //            self.playerV?.frame = [0,0,Swidth,(Swidth/2.0)]
                
                videoKit.player = self.playerV
                playerV?.completedPlayingBlock = {p in
                    p?.destroy()
                    videoKit.player = nil
                }
            }
            
        }else{
            //                        if let topV = videoKit as? TableTopVideoView
            //                        {
            //
            //                            self.playerV?.removeFromSuperview()
            //                            self.playerV?.destroy()
            //                            self.playerV = nil
            //
            //
            //                            self.playerV = XLVideoPlayer()
            //                            self.playerV?.videoUrl = cellModel["video",""]
            //                            self.playerV?.playerBindTableView(topV.tableView, currentIndexPath: IndexPath(row: 0, section: 0))
            //
            //                            videoKit.player = self.playerV
            //                            weak var wself = self
            //                            playerV?.completedPlayingBlock = {p in
            //                                p?.destroy()
            //                                wself?.playerV = nil
            //                            }
            //
            //                        }
            
        }
        
    }
    
}





